package com.dq.service.record.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.StaffUserChangeQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.record.StaffUserChangeRecord;
import com.dq.domain.record.WxUserChangeRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.repository.BaseRepositories;
import com.dq.repository.record.StaffUserChangeRecordRepository;
import com.dq.repository.record.WxUserChangeRecordRepository;
import com.dq.repository.user.StaffUserRepository;
import com.dq.repository.user.WxUserRepository;
import com.dq.service.org.OrgService;
import com.dq.service.record.StaffUserChangeRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;

/**
 * @Author luchangtong
 * @Date 2019/12/10 14:03
 */
@Service
public class StaffUserChangeRecordServiceImpl extends BaseRepositories implements StaffUserChangeRecordService {
    @Autowired
    private OrgService orgService;
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private WxUserRepository wxUserRepository;
    @Autowired
    private StaffUserRepository staffUserRepository;
    @Autowired
    private StaffUserChangeRecordRepository staffUserChangeRecordRepository;
    @Autowired
    private WxUserChangeRecordRepository wxUserChangeRecordRepository;
    @Override
    public List<StaffUserChangeRecord> findByWhere(StaffUserChangeQuery staffUserChangeQuery) {
        staffUserChangeQuery.format();
        Map<String, Object> query = staffUserChangeQuery.parseMap();
        if(staffUserChangeQuery.getOrgId() != null){
            Map<String, Object> keyQuery = new HashMap<>();
            keyQuery.put("originalOrg_id",staffUserChangeQuery.getOrgId());
            keyQuery.put("nowOrg_id",staffUserChangeQuery.getOrgId());
            query.put("keyQuery"+ BaseDaoImpl.orAndSqlStr, keyQuery);
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            Map<String, Object> keyQuery = new HashMap<>();
            keyQuery.put("originalOrg"+ BaseDaoImpl.inSqlStr,orgList);
            keyQuery.put("nowOrg"+ BaseDaoImpl.inSqlStr,orgList);
            query.put("keyQuery"+ BaseDaoImpl.orAndSqlStr, keyQuery);
        }
        if(staffUserChangeQuery.getStaffUserChangeType() != null){
            query.put("staffUserChangeType",staffUserChangeQuery.getStaffUserChangeType());
            query.remove("staffUserChangeType");
        }
        if(staffUserChangeQuery.getBeginTime() != null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr, staffUserChangeQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(staffUserChangeQuery.getEndTime() != null){
            query.put("createTime"+ BaseDaoImpl.lteSqlStr, staffUserChangeQuery.getEndTime());
            query.remove("endTime");
        }
        if(StringUtils.isNotEmpty(staffUserChangeQuery.getStaffName())){
            query.put("sysUser_userRealcName"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(staffUserChangeQuery.getStaffName()));
            query.remove("staffName");
        }
        List<StaffUserChangeRecord> staffUserChangeRecords = dao.find(dao.query2Hql(StaffUserChangeRecord.class,query),query);
        return staffUserChangeRecords;
    }

    @Override
    public Long countByWhere(StaffUserChangeQuery staffUserChangeQuery) {
        staffUserChangeQuery.format();
        Map<String, Object> query = staffUserChangeQuery.parseMap();
        if(staffUserChangeQuery.getOrgId() != null){
            Map<String, Object> keyQuery = new HashMap<>();
            keyQuery.put("originalOrg_id",staffUserChangeQuery.getOrgId());
            keyQuery.put("nowOrg_id",staffUserChangeQuery.getOrgId());
            query.put("keyQuery"+ BaseDaoImpl.orAndSqlStr, keyQuery);
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            Map<String, Object> keyQuery = new HashMap<>();
            keyQuery.put("originalOrg"+ BaseDaoImpl.inSqlStr,orgList);
            keyQuery.put("nowOrg"+ BaseDaoImpl.inSqlStr,orgList);
            query.put("keyQuery"+ BaseDaoImpl.orAndSqlStr, keyQuery);
        }
        if(staffUserChangeQuery.getStaffUserChangeType() != null){
            query.put("staffUserChangeType",staffUserChangeQuery.getStaffUserChangeType());
            query.remove("staffUserChangeType");
        }
        if(staffUserChangeQuery.getBeginTime() != null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr, staffUserChangeQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(staffUserChangeQuery.getEndTime() != null){
            query.put("createTime"+ BaseDaoImpl.lteSqlStr, staffUserChangeQuery.getEndTime());
            query.remove("endTime");
        }
        if(StringUtils.isNotEmpty(staffUserChangeQuery.getStaffName())){
            query.put("sysUser_userRealcName"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(staffUserChangeQuery.getStaffName()));
            query.remove("staffName");
        }
        Long count = dao.count(dao.count2Hql(StaffUserChangeRecord.class,query),query);
        return count;
    }

    @Override
    public WxUserChangeRecord save(WxUser wxUser, StaffUser staffUser, Org nowOrg, WxUserChangeRecord.WxUserChangeType wxUserChangeType) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        WxUserChangeRecord w = new WxUserChangeRecord();
        w.setSysUser(wxUser);
        w.setNowStaffUser(staffUser);
        w.setOriginalOrg((Org) wxUser.getSection());
        w.setNowOrg(nowOrg);
        w.setWxUserChangeType(wxUserChangeType);
        w.setCreateUser(sysUser);
        return w;
    }

    /**
     * 离职
     * @param staffUser
     * @throws AdminException
     */
    @Override
    public void staffQuit(StaffUser staffUser) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        staffUserRepository.save(staffUser);
        StaffUserChangeRecord s = new StaffUserChangeRecord();
        s.setOriginalOrg((Org) staffUser.getSection());
        s.setOriginalPosition(staffUser.getOrgPosition());
        s.setSysUser(staffUser);
        s.setStaffUserChangeType(StaffUserChangeRecord.StaffUserChangeType.quit);
        s.setCreateUser(sysUser);
        staffUserChangeRecordRepository.save(s);
    }

    /**
     * 调岗
     * @param
     * @param orgPosition
     * @param sysDept
     * @throws AdminException
     */
    @Override
    public void adjusting(OrgPosition orgPosition, Org sysDept, SysUser sysUser, StaffUserChangeRecord.StaffUserChangeType type) throws AdminException {
        StaffUser staffUser = dao.get(StaffUser.class,sysUser.getId());
        StaffUserChangeRecord s = new StaffUserChangeRecord();
        s.setOriginalOrg((Org)staffUser.getSection());
        s.setNowOrg(sysDept);
        s.setOriginalPosition(staffUser.getOrgPosition());
        s.setNowPosition(orgPosition);
        s.setSysUser(sysUser);
        s.setStaffUserChangeType(type);
        s.setCreateUser(shiroService.getUser());
        staffUserChangeRecordRepository.save(s);
        staffUser.setOrgPosition(orgPosition);
        staffUserRepository.save(staffUser);
    }

    public Query createWhereHql(StaffUserChangeQuery query, StringBuilder hql){
        if(query.getOrgId() != null){
            hql.append(" and (u.originalOrg =:sysDept");
            hql.append(" or u.nowOrg =:sysDept)");
        }
        if(query.getStaffUserChangeType() != null){
            hql.append(" and u.staffUserChangeType =:staffUserChangeType");
        }
        if(query.getBeginTime() != null){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(query.getEndTime() != null){
            hql.append(" and u.createTime <=:endTime");
        }
        if(StringUtils.isNotEmpty(query.getStaffName())){
            hql.append(" and u.sysUser.userRealcName like:staffName");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query q = entityManager.createQuery(hql.toString());
        if(query.getOrgId() != null){
            SysSection section = dao.get(SysSection.class,query.getOrgId());
            q.setParameter("sysDept", section);
        }
        if(query.getStaffUserChangeType() != null){
            q.setParameter("staffUserChangeType",query.getStaffUserChangeType());
        }
        if(query.getBeginTime() != null){
            q.setParameter("beginTime",query.getBeginTime());
        }
        if(query.getEndTime() != null){
            q.setParameter("endTime",query.getEndTime());
        }
        if(StringUtils.isNotEmpty(query.getStaffName())){
            q.setParameter("staffName","%"+query.getStaffName()+"%");
        }
        return q;
    }

    }
